package io.renren.modules.pet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.exception.RenException;
import io.renren.common.page.PageData;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.pet.dao.PetTeamDao;
import io.renren.modules.pet.entity.PetSendEntity;
import io.renren.modules.pet.service.PetTeamCollectService;
import io.renren.modules.pet.vo.PetCollectVO;
import io.renren.modules.pet.vo.PetSendVO;
import io.renren.modules.pet.vo.PetTeamCollectVO;
import io.renren.modules.pet.vo.PetTeamVO;
import io.renren.modules.pet.dto.PetTeamDTO;
import io.renren.modules.pet.entity.PetTeamEntity;
import io.renren.modules.pet.service.PetTeamService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.sys.entity.DictData;
import io.renren.modules.sys.entity.DictType;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 宠物团队信息
 *
 * @author zwp zwp
 * @since 1.0.0 2025-03-24
 */
@Service
public class PetTeamServiceImpl extends CrudServiceImpl<PetTeamDao, PetTeamEntity, PetTeamVO> implements PetTeamService {

    @Autowired
    private PetTeamCollectService petTeamCollectService;

    @Override
    public QueryWrapper<PetTeamEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String teamName = (String)params.get("teamName");

        QueryWrapper<PetTeamEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.like(StringUtils.isNotBlank(teamName), "team_name", teamName);
        wrapper.orderByDesc("create_date");

        return wrapper;
    }

    @Override
    public void save(PetTeamDTO petTeamDTO) {
        QueryWrapper<PetTeamEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("creator", SecurityUser.getUserId());
        Long count = this.baseDao.selectCount(wrapper);
        if (count.compareTo(0L) != 0) {
            throw new RenException("每个用户只能创建一个团队");
        }
        PetTeamEntity petTeamEntity = ConvertUtils.sourceToTarget(petTeamDTO, PetTeamEntity.class);
        this.insert(petTeamEntity);
    }

    @Override
    public void update(PetTeamDTO petTeamDTO) {
        PetTeamEntity petTeamEntity = ConvertUtils.sourceToTarget(petTeamDTO, PetTeamEntity.class);
        this.updateById(petTeamEntity);
    }

    @Override
    public List<PetTeamVO> getList(Map<String, Object> params) {
        String teamName = (String)params.get("teamName");
        QueryWrapper<PetTeamEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(teamName), "team_name", teamName);
        wrapper.eq("status", 1);
        List<PetTeamEntity> entityList = this.baseDao.selectList(wrapper);
        return ConvertUtils.sourceToTarget(entityList, PetTeamVO.class);
    }

    @Override
    public PetTeamVO getInfoById(Long id) {
        PetTeamEntity petTeamEntity = this.selectById(id);
        PetTeamVO petTeamVo = ConvertUtils.sourceToTarget(petTeamEntity, PetTeamVO.class);
        if (petTeamVo != null) {
            petTeamVo.setLogoUrlList(Arrays.asList(petTeamVo.getLogoUrl().split(",")));
            // 照片集合
            petTeamVo.setImageUrlList(Arrays.asList(petTeamVo.getImageUrl().split(",")));
            // 根据团队ID查询
            List<PetTeamCollectVO> teamCollectVoList = petTeamCollectService.getByPetTeamId(petTeamVo.getId());
            petTeamVo.setTeamCollectNum(teamCollectVoList.size());
            // 是否已收藏
            // 获取当前用户信息
            Long userId = SecurityUser.getUserId();
            PetTeamCollectVO petTeamCollectVo = teamCollectVoList.stream().filter(e -> e.getCreator().compareTo(userId) == 0).findFirst().orElse(null);
            if (petTeamCollectVo != null) {
                petTeamVo.setIsTeamCollect(1);
            } else {
                petTeamVo.setIsTeamCollect(0);
            }
        }
        return petTeamVo;
    }

    @Override
    public PageData<PetTeamVO> petTeamPage(Map<String, Object> params) {
        PageData<PetTeamVO> page = this.page(params);
        page.getList().forEach(e -> {
            if (StringUtils.isNotBlank(e.getImageUrl())) {
                // 照片集合
                e.setImageUrlList(Arrays.asList(e.getImageUrl().split(",")));
                e.setImageUrlSize(e.getImageUrlList().size());
            }
        });
        return page;
    }

    @Override
    public List<PetTeamVO> getTeamCollectByUser(Map<String, Object> params) {
        // 当前用户
        Long userId = SecurityUser.getUserId();
        // 获取当前用户的团队收藏表
        List<PetTeamCollectVO> petTeamCollectVoList = petTeamCollectService.getByUserId(userId);
        if (CollectionUtils.isEmpty(petTeamCollectVoList)) {
            return Collections.EMPTY_LIST;
        }
        QueryWrapper<PetTeamEntity> wrapper = new QueryWrapper<>();
        wrapper.in("id", petTeamCollectVoList.stream().map(e -> e.getPetTeamId()).distinct().collect(Collectors.toList()));
        wrapper.eq("status", 1);
        List<PetTeamEntity> entityList = this.baseDao.selectList(wrapper);
        return ConvertUtils.sourceToTarget(entityList, PetTeamVO.class);
    }

    @Override
    public PetTeamVO getTeamByUser(Map<String, Object> params) {
        QueryWrapper<PetTeamEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("creator", SecurityUser.getUserId());
        List<PetTeamEntity> entityList = this.baseDao.selectList(wrapper);
        if (CollectionUtils.isEmpty(entityList)) {
            return null;
        }
        return ConvertUtils.sourceToTarget(entityList.get(0), PetTeamVO.class);
    }

}